home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CLOBSH.PAK / BAG.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  428 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  BAG.H                                                                 */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991, 1993                            */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __BAG_H )
  11. #define __BAG_H
  12.  
  13. #define BI_OLDNAMES
  14.  
  15. #if !defined( TEMPLATES )
  16.  
  17. #if !defined( __RESOURCE_H )
  18. #include "classlib\Resource.h"
  19. #endif  // __RESOURCE_H
  20.  
  21. #if !defined( __CLSTYPES_H )
  22. #include "classlib\obsolete\ClsTypes.h"
  23. #endif  // __CLSTYPES_H
  24.  
  25. #if !defined( __HASHTBL_H )
  26. #include "classlib\obsolete\HashTbl.h"
  27. #endif  // __HASHTBL_H
  28.  
  29. #pragma option -Vo-
  30. #if defined( __BCOPT__ ) && !defined( __FLAT__ ) && !defined( _ALLOW_po )
  31. #pragma option -po-
  32. #endif
  33.  
  34. _CLASSDEF(Bag)
  35.  
  36. class _CLASSTYPE Bag : public Collection
  37. {
  38.  
  39. public:
  40.  
  41.     Bag( sizeType bagSize = DEFAULT_BAG_SIZE ) :
  42.         table( bagSize )
  43.         {
  44.         }
  45.  
  46.     virtual void add( Object _FAR & o )
  47.         {
  48.         table.add( o );
  49.         }
  50.  
  51.     virtual void detach( Object _FAR & o, DeleteType dt = NoDelete )
  52.         {
  53.         table.detach( o, dt );
  54.         }
  55.  
  56.     virtual void flush( DeleteType dt = DefDelete )
  57.         {
  58.         table.flush( dt );
  59.         }
  60.  
  61.     virtual int isEmpty() const
  62.         {
  63.         return table.isEmpty();
  64.         }
  65.  
  66.     virtual countType getItemsInContainer() const
  67.         {
  68.         return table.getItemsInContainer();
  69.         }
  70.  
  71.     void forEach( iterFuncType f, void _FAR *args )
  72.         {
  73.         table.forEach( f, args );
  74.         }
  75.  
  76.     Object _FAR& firstThat( condFuncType f, void _FAR *args ) const
  77.         {
  78.         return table.firstThat( f, args );
  79.         }
  80.  
  81.     Object _FAR& lastThat( condFuncType f, void _FAR *args ) const
  82.         {
  83.         return table.lastThat( f, args );
  84.         }
  85.  
  86.     virtual int hasMember( Object _FAR & o ) const
  87.         {
  88.         return table.hasMember( o );
  89.         }
  90.  
  91.     virtual Object _FAR & findMember( Object _FAR & o ) const
  92.         {
  93.         return table.findMember(o);
  94.         }
  95.  
  96.     virtual ContainerIterator _FAR & initIterator() const
  97.         {
  98.         return table.initIterator();
  99.         }
  100.  
  101.     virtual classType isA() const
  102.         {
  103.         return bagClass;
  104.         }
  105.  
  106.     virtual _TCHAR _FAR *nameOf() const
  107.         {
  108.         return "Bag";
  109.         }
  110.  
  111.     int ownsElements()
  112.         {
  113.         return table.ownsElements();
  114.         }
  115.  
  116.     void ownsElements( int del )
  117.         { 
  118.         table.ownsElements( del );
  119.         }
  120.  
  121. private:
  122.  
  123.     HashTable table;
  124.  
  125. };
  126.  
  127. #else   // TEMPLATES
  128.  
  129. #if !defined( __RESOURCE_H )
  130. #include "classlib\Resource.h"
  131. #endif  // __RESOURCE_H
  132.  
  133. #if !defined( __COLLECT_H )
  134. #include "classlib\obsolete\Collect.h"
  135. #endif  // __COLLECT_H
  136.  
  137. #if !defined( __BAGS_H )
  138. #include "classlib\Bags.h"
  139. #endif  // __BAGS_H
  140.  
  141. #pragma option -Vo-
  142. #if defined( __BCOPT__ ) && !defined( __FLAT__ ) && !defined( _ALLOW_po )
  143. #pragma option -po-
  144. #endif
  145.  
  146. #define Bag   BI_TCBagAsVector
  147. #define PBag  PBI_TCBagAsVector
  148. #define RBag  RBI_TCBagAsVector
  149. #define RPBag RPBI_TCBagAsVector
  150. #define PCBag PCBI_TCBagAsVector
  151. #define RCBag RCBI_TCBagAsVector
  152.  
  153. _CLASSDEF( BI_TCBagAsVector )
  154.  
  155. #define BagIterator   BI_TCBagAsVectorIterator
  156. #define PBagIterator  PBI_TCBagAsVectorIterator
  157. #define RBagIterator  RBI_TCBagAsVectorIterator
  158. #define RPBagIterator RPBI_TCBagAsVectorIterator
  159. #define PCBagIterator PCBI_TCBagAsVectorIterator
  160. #define RCBagIterator RCBI_TCBagAsVectorIterator
  161.  
  162. _CLASSDEF( BI_TCBagAsVectorIterator )
  163.  
  164. /*------------------------------------------------------------------------*/
  165. /*                                                                        */
  166. /*  class BI_OBagAsVector                                                 */
  167. /*                                                                        */
  168. /*  Implements a bag of pointers to Object,                               */
  169. /*  using a vector as the underlying implementation.                      */
  170. /*                                                                        */
  171. /*------------------------------------------------------------------------*/
  172.  
  173. class _CLASSTYPE BI_OBagAsVector
  174. {
  175.  
  176. public:
  177.  
  178.     friend class _CLASSTYPE BI_OBagAsVectorIterator;
  179.  
  180.     BI_OBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
  181.         obag( sz )
  182.         {
  183.         }
  184.  
  185.     void add( Object _FAR *o )
  186.         {
  187.         obag.add(o);
  188.         }
  189.  
  190.     void detach( Object _FAR *o,
  191.                  TShouldDelete::DeleteType dt = TShouldDelete::NoDelete
  192.                )
  193.         {
  194.         obag.detach( o, dt );
  195.         }
  196.  
  197.     void flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete )
  198.         {
  199.         obag.flush( dt );
  200.         }
  201.  
  202.     int hasMember( Object _FAR *o ) const
  203.         {
  204.         return obag.hasMember(o);
  205.         }
  206.  
  207.     Object _FAR *findMember( Object _FAR *o ) const
  208.         {
  209.         Object _FAR *obj = obag.findMember( o );
  210.         return obj != 0 ? obj : 0;
  211.         }
  212.  
  213.     int isEmpty() const
  214.         {
  215.         return obag.isEmpty();
  216.         }
  217.  
  218.     int isFull() const
  219.         {
  220.         return obag.isFull();
  221.         }
  222.  
  223.     void forEach( iterFuncType f, void _FAR*args )
  224.         {
  225.         obag.forEach( f, args );
  226.         }
  227.  
  228.     Object _FAR *firstThat( condFuncType f, void _FAR *args ) const
  229.         {
  230.         return obag.firstThat( f, args );
  231.         }
  232.  
  233.     Object _FAR *lastThat( condFuncType f, void _FAR *args ) const
  234.         {
  235.         return obag.lastThat( f, args );
  236.         }
  237.  
  238.     int getItemsInContainer() const
  239.         {
  240.         return obag.getItemsInContainer();
  241.         }
  242.  
  243.     int ownsElements()
  244.         {
  245.         return obag.ownsElements();
  246.         }
  247.  
  248.     void ownsElements( int del )
  249.         {
  250.         obag.ownsElements( del );
  251.         }
  252.  
  253. protected:
  254.  
  255.     BI_IBagAsVector<Object> obag;
  256.  
  257. };
  258.  
  259. class _CLASSTYPE BI_OBagAsVectorIterator :
  260.     public BI_IBagAsVectorIterator<Object>
  261. {
  262.  
  263. public:
  264.  
  265.     BI_OBagAsVectorIterator( const BI_OBagAsVector _FAR & b ) :
  266.         BI_IBagAsVectorIterator<Object>(b.obag)
  267.         {
  268.         }
  269.  
  270. };
  271.  
  272. /*------------------------------------------------------------------------*/
  273. /*                                                                        */
  274. /*  class BI_TCBagAsVector                                                */
  275. /*                                                                        */
  276. /*  Implements an Object bag, with the full semantics of                  */
  277. /*  the BC 2.0 style Bag, using a vector as the underlying                */
  278. /*  implementation.                                                       */
  279. /*                                                                        */
  280. /*------------------------------------------------------------------------*/
  281.  
  282. class _CLASSTYPE BI_TCBagAsVector : public Collection
  283. {
  284.  
  285. public:
  286.  
  287.     friend class _CLASSTYPE BI_TCBagAsVectorIterator;
  288.  
  289.     BI_TCBagAsVector( int sz = DEFAULT_BAG_SIZE ) :
  290.         bag(sz)
  291.         {
  292.         }
  293.  
  294.     virtual void add( Object _FAR & o )
  295.         {
  296.         bag.add( &o );
  297.         }
  298.  
  299.     virtual void detach( Object _FAR & o,
  300.                     TShouldDelete::DeleteType dt = TShouldDelete::NoDelete )
  301.         {
  302.         bag.detach( &o, dt );
  303.         }
  304.  
  305.     virtual void flush(TShouldDelete::DeleteType dt=TShouldDelete::DefDelete )
  306.         {
  307.         bag.flush( dt );
  308.         }
  309.  
  310.     virtual int isEmpty() const
  311.         {
  312.         return bag.isEmpty();
  313.         }
  314.  
  315.     virtual countType getItemsInContainer() const
  316.         {
  317.         return bag.getItemsInContainer();
  318.         }
  319.  
  320.     void forEach( iterFuncType f, void _FAR*args )
  321.         {
  322.         bag.forEach( f, args );
  323.         }
  324.  
  325.     Object _FAR & firstThat( condFuncType f, void _FAR *args ) const
  326.         {
  327.         return ptrToRef(bag.firstThat( f, args ));
  328.         }
  329.  
  330.     Object _FAR & lastThat( condFuncType f, void _FAR *args ) const
  331.         {
  332.         return ptrToRef(bag.lastThat( f, args ));
  333.         }
  334.  
  335.     virtual int hasMember( Object _FAR & o ) const
  336.         {
  337.         return bag.hasMember( &o );
  338.         }
  339.  
  340.     virtual Object _FAR & findMember( Object _FAR & o ) const
  341.         {
  342.         return ptrToRef(bag.findMember(&o));
  343.         }
  344.  
  345.     virtual ContainerIterator _FAR & initIterator() const;
  346.  
  347.     virtual classType isA() const
  348.         {
  349.         return bagClass;
  350.         }
  351.  
  352.     virtual _TCHAR _FAR *nameOf() const
  353.         {
  354.         return "BI_TCBagAsVector";
  355.         }
  356.  
  357.     int ownsElements()
  358.         {
  359.         return bag.ownsElements();
  360.         }
  361.  
  362.     void ownsElements( int del )
  363.         {
  364.         bag.ownsElements( del );
  365.         }
  366.  
  367. protected:
  368.  
  369.     BI_OBagAsVector bag;
  370.  
  371. };
  372.  
  373. class _CLASSTYPE BI_TCBagAsVectorIterator : public ContainerIterator
  374. {
  375.  
  376. public:
  377.  
  378.     BI_TCBagAsVectorIterator( const BI_TCBagAsVector _FAR &b ) :
  379.         iter(b.bag)
  380.         {
  381.         }
  382.  
  383.     virtual operator int()
  384.         {
  385.         return int(iter);
  386.         }
  387.  
  388.     virtual Object _FAR & current()
  389.         {
  390.         return Object::ptrToRef(iter.current());
  391.         }
  392.  
  393.     virtual Object _FAR & operator ++ ( int )
  394.         {
  395.         return Object::ptrToRef(iter++);
  396.         }
  397.  
  398.     virtual Object _FAR & operator ++ ()
  399.         {
  400.         return Object::ptrToRef(++iter);
  401.         }
  402.  
  403.     virtual void restart()
  404.         {
  405.         iter.restart();
  406.         }
  407.  
  408. private:
  409.  
  410.     BI_OBagAsVectorIterator iter;
  411.  
  412. };
  413.  
  414. inline ContainerIterator _FAR & BI_TCBagAsVector::initIterator() const
  415. {
  416.     return *new BI_TCBagAsVectorIterator( *this );
  417. }
  418.  
  419. #endif  // TEMPLATES
  420.  
  421. #if defined( __BCOPT__ ) && !defined( __FLAT__ ) && !defined( _ALLOW_po )
  422. #pragma option -po.
  423. #endif
  424. #pragma option -Vo.
  425.  
  426. #endif  // __BAG_H
  427.  
  428.